package org.lanqiao.controller;

import org.lanqiao.entity.Priv;
import org.lanqiao.entity.Role;
import org.lanqiao.service.PrivService;
import org.lanqiao.service.RoleService;
import org.lanqiao.service.impl.PrivSerivceImpl;
import org.lanqiao.service.impl.RoleServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
public class RoleController {
    @Resource
    RoleService roleService;
    @Resource
    PrivService privService;

    @RequestMapping("/role/RoleListAction.do")
    protected void Rolelist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Role> lr = roleService.getAllRoles();
        request.setAttribute("lr", lr);
        request.getRequestDispatcher("/role/role_list.jsp").forward(request, response);
    }

    @RequestMapping("/RolePrivsAction.do")
    protected void showRoleAdd(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        List<Priv> lp = privService.getAllPrivs();
        request.setAttribute("lp", lp);
        request.getRequestDispatcher("/role/role_add.jsp").forward(request, response);
    }

    @RequestMapping("/RoleAddAction.do")
    public void roleAddAction(HttpServletRequest request, @RequestParam("by001") MultipartFile file, HttpServletResponse response) throws Exception {
        String rname = request.getParameter("rname");
        String[] privs = request.getParameterValues("privs");
        // 保存图片的路径，图片上传成功后，将路径保存到数据库
        String filePath = request.getServletContext().getRealPath("/upload");
        ;
        // 获取原始图片的扩展名
        String originalFilename = file.getOriginalFilename();
        // 生成文件新的名字
        String newFileName = originalFilename;
        // 封装上传文件位置的全路径
        File targetFile = new File(filePath, newFileName);
        file.transferTo(targetFile);
        Role role = new Role();
        role.setRname(rname);
        role.setBy001("/upload/" + newFileName);
        List<Priv> lp = new ArrayList<Priv>();
        for (String pid : privs) {
            lp.add(new Priv(Integer.parseInt(pid)));
        }
        role.setLp(lp);
        boolean b = roleService.insertRole(role);
        if (b) {
            //添加成功
            request.setAttribute("state", "1");
        } else {
            request.setAttribute("state", "0");
        }
        request.getRequestDispatcher("/RolePrivsAction.do").forward(request, response);
    }
    @RequestMapping("/ShowUpdateRole.do")
    public void showupdaterole(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String rid = request.getParameter("rid");
        Role role = roleService.getRoleByRid(Integer.parseInt(rid));
        List<Priv> lp = privService.getAllPrivs();
        request.setAttribute("role", role);
        request.setAttribute("lp", lp);
        request.getRequestDispatcher("/role/role_modi.jsp").forward(request, response);
    }
    @RequestMapping("/updateRole.do")
    public void updaterole(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String rid = request.getParameter("rid");
        String rname = request.getParameter("rname");
        String[] privs = request.getParameterValues("privs");

        Role role = new Role();
        role.setId(Integer.parseInt(rid));
        role.setRname(rname);
        List<Priv> lp = new ArrayList<>();
        for (String p : privs) {
            Priv priv = new Priv(Integer.parseInt(p));
            lp.add(priv);
        }
        role.setLp(lp);

        boolean b = roleService.updateRole(role);
        if (b) {
            //添加成功
            request.setAttribute("state", "1");
        } else {
            //添加失败
            request.setAttribute("state", "0");
        }
        request.getRequestDispatcher("/ShowUpdateRole.do").forward(request, response);
    }
    @RequestMapping("/RoleDelAction.do")
    public void delrole(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String rid = request.getParameter("rid");
        boolean b = roleService.deleteRole(Integer.parseInt(rid));
        if(b) {
            request.setAttribute("state", "1");
        }else {
            request.setAttribute("state", "0");
        }
        request.getRequestDispatcher("/role/RoleListAction.do").forward(request, response);
    }
    }




